package net.m2technologies.open_arm.samples.pojo;

import net.m2technologies.open_arm.OpenArmConfiguration;
import net.m2technologies.open_arm.OpenArmTransactionSimpleFacade;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.opengroup.arm40.transaction.ArmConstants;
import org.opengroup.arm40.transaction.ArmTransaction;

/**
 * Copyright 2005 Mark Masterson<br> <br> Licensed under the Apache License, Version 2.0 (the "License");<br> you may
 * not use this file except in compliance with the License.<br> You may obtain a copy of the License at<br> <br>
 * http://www.apache.org/licenses/LICENSE-2.0<br> <br> Unless required by applicable law or agreed to in writing,
 * software<br> distributed under the License is distributed on an "AS IS" BASIS,<br> WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, either express or implied.<br> See the License for the specific language governing permissions and<br>
 * limitations under the License.<br>
 * <p/>
 * <p>Description: </p>
 *
 * @author Mark Masterson
 * @version 0.010
 */
public abstract class AbstractPojoApplication {

    private static final Logger logger = Logger.getLogger(AbstractPojoApplication.class);

    static {
        BasicConfigurator.configure();
        logger.getLoggerRepository().setThreshold("ALL");
        logger.info("[initializeLogging] Configured!");
    }

    private final OpenArmTransactionSimpleFacade openArm;

    protected AbstractPojoApplication(final String configurationFile) {
        // Use the OpenArmTransactionSimpleFacade class, to simplify usage of the API
        this.openArm = new OpenArmTransactionSimpleFacade(configurationFile, "Simple Application Sample");
    }

    protected AbstractPojoApplication(final OpenArmConfiguration config) {
        // Use the OpenArmTransactionSimpleFacade class, to simplify usage of the API
        this.openArm = new OpenArmTransactionSimpleFacade(config, "Simple Application Sample");
    }

    public void doSomething() {
        // Get a transaction monitor, passing the name of the method as the name of the transaction
        final ArmTransaction transactionMonitor = this.openArm.getArmTransactionMonitor("doSomething");
        // Tell ARM that things are starting
        transactionMonitor.start();
        doSomeBusinessStuff();
        // Tell ARM that we're done, and everything went well...
        transactionMonitor.stop(ArmConstants.STATUS_GOOD);
    }

    public void doSomethingWithSetupOverhead() {
        // Get a transaction monitor, passing the name of the method as the name of the transaction
        final ArmTransaction transactionMonitor = this.openArm.getArmTransactionMonitor("doSomethingWithSetupOverhead");
        // Tell ARM that we've arrived, but things haven't yet really started...
        transactionMonitor.setArrivalTime();
        doSomeSetupStuff();
        // Tell ARM that things are starting
        transactionMonitor.start();
        doSomeBusinessStuff();
        // Tell ARM that we're done, and everything went well...
        transactionMonitor.stop(ArmConstants.STATUS_GOOD);
    }

    public void doSomethingWithExceptionHandling() {
        // Get a transaction monitor, passing the name of the method as the name of the transaction
        final ArmTransaction transactionMonitor = this.openArm.getArmTransactionMonitor(
                "doSomethingWithExceptionHandling");
        // Tell ARM that things are starting
        transactionMonitor.start();
        try {
            doSomeBusinessStuffThatThrowsExceptions();
            // Tell ARM that we're done, and everything went well...
            transactionMonitor.stop(ArmConstants.STATUS_GOOD);
        } catch (Exception e) {
            e.printStackTrace();
            // Tell ARM that we're done, and that things went poorly...
            transactionMonitor.stop(ArmConstants.STATUS_FAILED);
        }
    }

    protected void doSomeBusinessStuff() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    protected void doSomeSetupStuff() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    protected void doSomeBusinessStuffThatThrowsExceptions() throws Exception {
        Thread.sleep(2000);
    }

}
